package com.atguigu.daijia.coupon.client.fallback;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.coupon.client.CouponFeignClient;
import com.atguigu.daijia.model.form.coupon.UseCouponForm;
import com.atguigu.daijia.model.vo.base.PageVo;
import com.atguigu.daijia.model.vo.coupon.AvailableCouponVo;
import com.atguigu.daijia.model.vo.coupon.NoReceiveCouponVo;
import com.atguigu.daijia.model.vo.coupon.NoUseCouponVo;
import com.atguigu.daijia.model.vo.coupon.UsedCouponVo;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @create: 2024/10/3
 * @author: gyhe10
 * @description: CouponFeignClient 接口的降级实现工厂类
 **/

@Component
public class CouponFeignClientFallbackFactory implements FallbackFactory<CouponFeignClient> {
    @Override
    public CouponFeignClient create(Throwable cause) {
        return new CouponFeignClient() {
            @Override
            public Result<PageVo<NoReceiveCouponVo>> findNoReceiveCouponPage(Long customerId, Long page, Long limit) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<PageVo<NoUseCouponVo>> findNoUseCouponPage(Long customerId, Long page, Long limit) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<PageVo<UsedCouponVo>> findUsedCouponPage(Long customerId, Long page, Long limit) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<Boolean> receiveCoupon(Long customerId, Long couponId) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<List<AvailableCouponVo>> findAvailableCouponList(Long customerId, BigDecimal orderAmount) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<BigDecimal> useCoupon(UseCouponForm useCouponForm) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }
        };
    }
}
